package com.itboy.mnt.database.controller;

import java.util.List;
import java.util.Arrays;

import com.itboy.mnt.database.domain.MntDatabase;
import lombok.RequiredArgsConstructor;
import javax.validation.constraints.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.itboy.common.annotation.RepeatSubmit;
import com.itboy.common.annotation.Log;
import com.itboy.common.core.controller.BaseController;
import com.itboy.common.core.domain.AjaxResult;
import com.itboy.common.enums.BusinessType;
import com.itboy.mnt.database.vo.MntDatabaseVo;
import com.itboy.mnt.database.bo.MntDatabaseQueryBo;
import com.itboy.mnt.database.bo.MntDatabaseAddBo;
import com.itboy.mnt.database.bo.MntDatabaseEditBo;
import com.itboy.mnt.database.service.IMntDatabaseService;
import com.itboy.common.utils.poi.ExcelUtil;
import com.itboy.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 数据库管理Controller
 *
 * @author itboy
 * @date 2021-07-29
 */
@Validated
@Api(value = "数据库管理控制器", tags = {"数据库管理管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/mnt/database")
public class MntDatabaseController extends BaseController {

    private final IMntDatabaseService iMntDatabaseService;

    /**
     * 查询数据库管理列表
     */
    @ApiOperation("查询数据库管理列表")
    @PreAuthorize("@ss.hasPermi('mnt:database:list')")
    @GetMapping("/list")
    public TableDataInfo<MntDatabaseVo> list(@Validated MntDatabaseQueryBo bo) {
        return iMntDatabaseService.queryPageList(bo);
    }

    /**
     * 导出数据库管理列表
     */
    @ApiOperation("导出数据库管理列表")
    @PreAuthorize("@ss.hasPermi('mnt:database:export')")
    @Log(title = "数据库管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<MntDatabaseVo> export(@Validated MntDatabaseQueryBo bo) {
        List<MntDatabaseVo> list = iMntDatabaseService.queryList(bo);
        ExcelUtil<MntDatabaseVo> util = new ExcelUtil<MntDatabaseVo>(MntDatabaseVo.class);
        return util.exportExcel(list, "数据库管理");
    }

    /**
     * 获取数据库管理详细信息
     */
    @ApiOperation("获取数据库管理详细信息")
    @PreAuthorize("@ss.hasPermi('mnt:database:query')")
    @GetMapping("/{dbId}")
    public AjaxResult<MntDatabaseVo> getInfo(@NotNull(message = "主键不能为空")
                                                  @PathVariable("dbId") Long dbId) {
        return AjaxResult.success(iMntDatabaseService.queryById(dbId));
    }

    /**
     * 新增数据库管理
     */
    @ApiOperation("新增数据库管理")
    @PreAuthorize("@ss.hasPermi('mnt:database:add')")
    @Log(title = "数据库管理", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated @RequestBody MntDatabaseAddBo bo) {
        return toAjax(iMntDatabaseService.insertByAddBo(bo) ? 1 : 0);
    }

    /**
     * 修改数据库管理
     */
    @ApiOperation("修改数据库管理")
    @PreAuthorize("@ss.hasPermi('mnt:database:edit')")
    @Log(title = "数据库管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated @RequestBody MntDatabaseEditBo bo) {
        return toAjax(iMntDatabaseService.updateByEditBo(bo) ? 1 : 0);
    }

    /**
     * 删除数据库管理
     */
    @ApiOperation("删除数据库管理")
    @PreAuthorize("@ss.hasPermi('mnt:database:remove')")
    @Log(title = "数据库管理" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{dbIds}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] dbIds) {
        return toAjax(iMntDatabaseService.deleteWithValidByIds(Arrays.asList(dbIds), true) ? 1 : 0);
    }

	/**
	 * 修改数据库管理
	 */
	@ApiOperation("测试数据库链接")
	@PreAuthorize("@ss.hasPermi('mnt:database:test')")
	@Log(title = "测试数据库链接", businessType = BusinessType.UPDATE)
	@RepeatSubmit
	@PostMapping("/testConnection")
	public AjaxResult<Void> testConnection(@Validated @RequestBody MntDatabase mntDatabase) {
		return toAjax(iMntDatabaseService.testConnection(mntDatabase) ? 1 : 0);
	}
}
